package com.gitee.base;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.context.ApplicationContextHolder;

/**
 * 控制器基类
 * 
 * @author 赵炎
 * @version [V1.00, 2019年1月7日]
 * @since V1.00
 */
public abstract class BaseController<T extends BaseModel, S extends BaseService<T>> extends AbstractController
{
    
    /**
     * service服务
     */
    protected S service;
    
    @SuppressWarnings("unchecked")
    @Override
    public void afterPropertiesSet() throws Exception
    {
        if (service == null)
        {
            ParameterizedType type = (ParameterizedType)getClass().getGenericSuperclass();
            Class<?> clazz = (Class<?>)type.getActualTypeArguments()[1];
            try
            {
                // 获取Service
                service = (S)ApplicationContextHolder.getService(clazz);
            }
            catch (Exception e)
            {
                log.error("加载异常，重新加载", e);
                // afterPropertiesSet();
            }
        }
        // TODO RPC modes need config
        super.afterPropertiesSet();
    }
    
    protected IPage<T> list(T entity)
    {
        // TODO 分页参数获取
        Page<T> page = new Page<T>(1, 20);
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>(entity);
        return this.service.page(page, queryWrapper);
    }
    
    /**
     * 添加保存
     * 
     * @param entity
     *            实体类
     * @return
     */
    protected Result addSave(T entity)
    {
        boolean flag = this.service.save(entity);
        if (flag)
        {
            return Result.success();
        }
        else
        {
            return Result.error("保存异常");
        }
    }
    
    /**
     * 修改保存
     * 
     * @param entity
     *            实体类
     * @return
     */
    public Result editSave(T entity)
    {
        boolean flag = this.service.updateById(entity);
        if (flag)
        {
            return Result.success();
        }
        else
        {
            return Result.error("修改异常");
        }
    }
    
    /**
     * （批量）删除
     * 
     * @param ids
     * @return
     */
    public Result remove(String ids)
    {
        String[] idArr = ids.split(",");
        List<Long> idList = new ArrayList<Long>();
        for (String id : idArr)
        {
            idList.add(Long.valueOf(id));
        }
        boolean flag = this.service.removeByIds(idList);
        if (flag)
        {
            return Result.success();
        }
        else
        {
            return Result.error("删除异常");
        }
    }
    
    /**
     * 不同的控制器页面
     * 此方法需要重写
     * 
     * @return
     */
    public abstract String view();
}
